/**

typedef struct {
    char* name;
    int id;
} Task;

void printTask(Any  data) {
    Task* t = (Task*)data;
    printf("%s(id:%d)", t->name, t->id);
}

void hashmap_str_print(HashMap *map) {
    if (!map) return;
    for (size_t i = 0; i < map->capacity; i++) {
        printf("[%d] ", i);
        kvPair *current = map->buckets[i];
        while (current) {
            TopoNode *node = (TopoNode*)current->value;
            Task* task = (Task*)node->data;
            printf("{%s->%d} ", task->name, task->id);
            current = current->next;
        }
        printf("\n");
    }
}

int main() {
    Task tasks[] = {
        {"Initialize", 1},
        {"LoadConfig", 2},
        {"ConnectDB", 3},
        {"StartService", 4},
        {"RunTests", 5},
        {"kkkkkkk",6}
    };

    // 创建hashmap图
    HashMap* graph = hashmap_create(11, 128, sizeof(TopoNode), string_hash, string_compare, NULL, NULL);
    // 添加节点
    for (int i = 0; i < 6; i++) {
        TopoNode* node = (TopoNode*)malloc(sizeof(TopoNode));
        node->data = &tasks[i];
        printf("添加节点：%s -> %d\n", tasks[i].name,tasks[i].id);
        node->indegree = 0;
        node->adj = NULL;
        hashmap_put(graph, tasks[i].name, node);
    }
    // 建立依赖关系
    printf("建立依赖关系:\n");
    addDependence(graph, tasks[0].name, tasks[1].name); // 1 -> 2
    addDependence(graph, tasks[0].name, tasks[2].name); // 1 -> 3
    addDependence(graph, tasks[1].name, tasks[3].name); // 2 -> 4
    addDependence(graph, tasks[2].name, tasks[3].name); // 3 -> 4
    addDependence(graph, tasks[3].name, tasks[4].name); // 4 -> 5
    addDependence(graph, tasks[3].name, tasks[5].name); // 4 -> 6
    addDependence(graph, tasks[4].name, tasks[5].name); // 5 -> 6
    printf("图中节点数量：%d\n", graph->size);
    // hashmap_str_print(graph);
    // 计算拓扑排序
    TopoSortResult result = {NULL,NULL,0,0};
    hashmap_topo_sort(graph, &result);
    
    hashmap_str_print(graph);
    // 打印结果
    if (result.size == 0)
    {
        printf("图中存在环路！\n");
    }else {
        printf("拓扑排序结果（总批次：%d）:\n", result.totalBatches);
        for (size_t i = 0; i < result.size; i++) {
            printTask(result.order[i]);
            printf(" -> 批次 %d\n", result.batches[i]);
        }
        free(result.order);
        free(result.batches);
    }
    return 0;
}
*/
#ifndef _MAP_H_
#define _MAP_H_

typedef void* Any;          // 通用数据类型
typedef void (*free_kvPair)(void* , void* );  // 释放函数类型

// 哈希表节点
typedef struct kvPair {
    void *key;          // 键指针
    void *value;        // 值指针
    struct kvPair *next;  // 下一个节点
} kvPair;

// 哈希表结构
typedef struct {
    kvPair **buckets;   // kv桶
    size_t capacity;    // 总容量
    size_t size;        // 当前元素数量
    size_t keySize;    // 键类型大小
    size_t valueSize;    // 值类型大小
    /*hashMap函数指针*/

    unsigned long (*hash_func)(const void *key);            // 哈希函数
    int (*key_compare)(const void *key1, const void *key2); // 键比较函数
    void (*key_free)(void *key);                            // 键释放函数
    void (*value_free)(void *value);                        // 值释放函数
} HashMap;

// 邻接表
typedef struct AdjTable {
    void*           dest;
    struct AdjTable *next;
} AdjTable;

// topo节点
typedef struct TopoNode {
    Any data;           // 节点数据
    int indegree;       // 入度值（原始）
    AdjTable *adj;      // 邻接表
} TopoNode;

// topo依赖关系
typedef struct TopoDependece {
    Any* addDependence;
    struct TopoDependece *next;
} TopoDependece;

// topo排序结果
typedef struct  TopoSortResult {
    Any* order;              // 排序结果数组
    int* batches;            // 批次标记数组
    TopoDependece* requires; // 强依赖关系
    TopoDependece* wants;    // 弱依赖关系
    size_t size;             // 结果总数
    int totalBatches;        // 总批次数
} TopoSortResult;

typedef struct  batchResult {
    Any*   data;            // 节点数组
    int count;                   // 节点数量
    int batch;                   // 批次号
    struct batchResult* next;
} BatchResult;
/*----------- 哈希函数 -----------*/

unsigned long int_hash(const void *key);
unsigned long string_hash(const void *key);

/*----------- key比较函数 -----------*/

int int_compare(const void *key1, const void *key2);
int string_compare(const void *key1, const void *key2);

/*---------- 哈希表操作 ----------*/

HashMap* hashmap_create(
    size_t capacity,
    size_t keySize,
    size_t valueSize,
    unsigned long (*hash_func)(const void *),
    int (*key_compare)(const void *, const void *),
    void (*key_free)(void *),
    void (*value_free)(void *)
);
void hashmap_destroy(HashMap *map);
int hashmap_put(HashMap *map, const void *key, const void *value);
void* hashmap_get(HashMap *map, const void *key);
int hashmap_remove(HashMap *map, const void *key, free_kvPair free_kv);
/*---------- 哈希拓扑操作 ----------*/

int hashmap_add_topo_node(HashMap* graph, const void* key, Any data);
int hashmap_add_topo_dependence(HashMap* graph, const void*  srcKey, const void* destKey);
int hashmap_topo_sort(HashMap* graph, TopoSortResult* result);

#endif /* _MAP_H_ */